{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Variables are Memory References"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can find the memory address that a variable *references*, by using the `id()` function.\n",
    "\n",
    "The `id()` function returns the memory address of its argument as a base-10 integer.\n",
    "\n",
    "We can use the function `hex()` to convert the base-10 number to base-16."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my_var = 10\n",
      "memory address of my_var (decimal): 1968827120\n",
      "memory address of my_var (hex): 0x7559eaf0\n"
     ]
    }
   ],
   "source": [
    "my_var = 10\n",
    "print('my_var = {0}'.format(my_var))\n",
    "print('memory address of my_var (decimal): {0}'.format(id(my_var)))\n",
    "print('memory address of my_var (hex): {0}'.format(hex(id(my_var))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "greeting = Hello\n",
      "memory address of my_var (decimal): 1688681719264\n",
      "memory address of my_var (hex): 0x1892d4625e0\n"
     ]
    }
   ],
   "source": [
    "greeting = 'Hello'\n",
    "print('greeting = {0}'.format(greeting))\n",
    "print('memory address of my_var (decimal): {0}'.format(id(greeting)))\n",
    "print('memory address of my_var (hex): {0}'.format(hex(id(greeting))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note how the memory address of `my_var` is **different** from that of `greeting`.\n",
    "\n",
    "Strictly speaking, `my_var` is not \"equal\" to 10. \n",
    "\n",
    "Instead `my_var` is a **reference** to an (*integer*) object (*containing the value 10*) located at the memory address `id(my_var)`\n",
    "\n",
    "Similarly for the variable `greeting`."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
